package com.gears.bachUpload.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.csource.test.FastDFSFile;
import org.csource.test.FileManager;
import org.springframework.stereotype.Service;

import com.gears.Constant;


@Service(value= "hehe")
public class BachUploadService {
	
	@Resource(name = "fileService")
	private FileService fileService;
	
	private Logger logger = Logger.getLogger(this.getClass());
	
	public void run(){
		
		String videoUrl = null;
		String title =null;
		String id = null;
		//查找第一个资源，修改状态为1
		synchronized (fileService) {
			List<Map<String, Object>> files_info_not_handle = fileService.getResouceNotHandle();
			if(files_info_not_handle.isEmpty())
				return;
			Map<String, Object> file_info_not_handle = files_info_not_handle.get(0);
			videoUrl = (String) file_info_not_handle.get("VIDEO1URL");
			title= (String) file_info_not_handle.get("TITLE");
			id= (String) file_info_not_handle.get("RESOURCEID");
			logger.info("videoUrl:"+videoUrl+"---title:"+title+"---sourceId:"+id);
			fileService.makeResourceHandling(id);
		}
//		
//		File content = new File(Constant.file_folder_path+videoUrl);
//		FileInputStream fis = null;
//		try {
//			fis = new FileInputStream(content);
//		} catch (FileNotFoundException e) {
//			logger.error("获取文件错误",e);
//			return;
//		}
//		//读取文件流，传输文件，得到文件地址，改成2
//		byte[] file_buff = null;
//		    if (fis != null) {
//		    	int len;
//				try {
//					len = fis.available();
//					file_buff = new byte[len];
//			    	fis.read(file_buff);
//				} catch (IOException e) {
//					logger.error("输入输出错误",e);
//					fileService.returnStatus(id);
//					return;
//				}finally{
//					 try {
//							fis.close();
//					 } catch (IOException e) {
//							logger.error("流关闭错误",e);
//					 }
//				}
//		    
//		    }
//		  FastDFSFile file = new FastDFSFile(title, file_buff, videoUrl.substring(videoUrl.lastIndexOf(".") + 1, videoUrl.length()));
		  FastDFSFile file = new FastDFSFile(Constant.getConstantVal(Constant.key_file_folder_path)+videoUrl, null, videoUrl.substring(videoUrl.lastIndexOf(".") + 1, videoUrl.length()));
		  String fileAbsolutePath = null;
		  try {
			  fileAbsolutePath =   FileManager.upload1(file);
		} catch (Exception e) {
			logger.error("传输出错",e);
			fileService.returnStatus(id);
			return;
		}
		  logger.info("fastDfS http url:"+fileAbsolutePath+"---source_id:"+id);
		  fileService.updateFileInfo(id,fileAbsolutePath);
		
	}
	
	
	
}
